package com.molichuxing.framework.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @prouect_name: framework
 * @class_name: StringUtil
 * @description: 字符串工具类
 * @author: 林亿建
 * @create_date: 2017年08月02日
 * @modify_date: 2017年08月02日
 **/
public class StringUtil {
	public static final String SLASH = getSlash();
	public static final String BACKSLASH = getBackslash();
	public static final String URL_SLASH = getURLSlash();
	public static final String BASE64_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	private static final int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };

	private static final int[] vi = { 1, 0, 88, 9, 8, 7, 6, 5, 4, 3, 2 };
	private static int[] ai = new int[18];

	public static String getSlash() {
		return "/";
	}

	public static String getBackslash() {
		return "\\";
	}

	public static String getURLSlash() {
		return "//";
	}

	public static String getHTMLBlank() {
		return " ";
	}

	// 判断是否为空
	public static boolean isEmpty(String string) {
		return (string == null) || (string.length() == 0);
	}

	// 比较
	public static boolean compare(String a, String b) {
		if ((isEmpty(a)) && (isEmpty(b)))
			return true;
		if ((!isEmpty(a)) && (!isEmpty(b))) {
			return a.equals(b);
		}
		return false;
	}

	// 去除大小写比较
	public static boolean compareIgnoreCase(String a, String b) {
		if ((isEmpty(a)) && (isEmpty(b)))
			return true;
		if ((!isEmpty(a)) && (!isEmpty(b))) {
			return a.equalsIgnoreCase(b);
		}
		return false;
	}

	// 复制
	public static String copy(String src, int len) {
		if (src == null)
			return null;
		if (src.length() > len) {
			return (len <= 0) ? null : src.substring(0, len);
		}
		return src;
	}

	// 删除某字符串
	public static String delete(String src, String delStr) {
		if ((isEmpty(src)) || (isEmpty(delStr)))
			return src;
		StringBuffer buffer = new StringBuffer(src);
		for (int index = src.length(); (index = src.lastIndexOf(delStr, index)) >= 0;) {
			buffer.delete(index, index + delStr.length());

			index = index - delStr.length();
		}

		return buffer.toString();
	}

	// 插入
	public static String insert(String src, String anotherStr, int offset) {
		if ((isEmpty(src)) || (isEmpty(anotherStr)))
			return src;
		StringBuffer buffer = new StringBuffer(src);
		if ((offset >= 0) && (offset <= src.length()))
			buffer.insert(offset, anotherStr);
		return buffer.toString();
	}

	// 追加
	public static String append(String src, String appendStr) {
		if ((isEmpty(src)) || (isEmpty(appendStr))) {
			return src;
		}
		StringBuffer buffer = new StringBuffer(src);
		buffer.append(appendStr);
		return buffer.toString();
	}

	// 替换
	public static String replace(String src, String oldStr, String newStr, boolean isCaseSensitive) {
		if ((isEmpty(src)) || (isEmpty(oldStr)) || (newStr == null))
			return src;
		String s = (isCaseSensitive) ? src : src.toLowerCase();
		String o = (isCaseSensitive) ? oldStr : oldStr.toLowerCase();
		StringBuffer buffer = new StringBuffer(src);
		for (int index = s.length(); (index = s.lastIndexOf(o, index)) >= 0;) {
			buffer.replace(index, index + o.length(), newStr);

			index = index - o.length();
		}

		return buffer.toString();
	}

	public static String replace(String src, String oldStr, String newStr, boolean isCaseSensitive, int index) {
		if ((src == null) || (src.length() == 0) || (oldStr == null) || (oldStr.length() == 0) || (index <= 0))
			return src;
		if (newStr == null)
			newStr = "";
		String s = (isCaseSensitive) ? src : src.toLowerCase();
		String o = (isCaseSensitive) ? oldStr : oldStr.toLowerCase();
		StringBuffer buffer = new StringBuffer(src);
		int length = o.length();

		int pos;
		for (pos = s.indexOf(o, 0); pos >= 0; pos = s.indexOf(o, pos + length))
			if (--index == 0)
				break;

		if (pos >= 0 && index == 0)
			buffer.replace(pos, pos + length, newStr);
		return buffer.toString();
	}

	public static String[] split(String str, String delimiter) {
		ArrayList array = new ArrayList();
		int index = 0;
		int begin = 0;
		String[] result = new String[0];
		if (isEmpty(str))
			return result;
		while (true) {
			index = str.indexOf(delimiter, begin);
			if (index == begin) {
				if (index >= 0)
					array.add("");
				begin += delimiter.length();
			}

			if (index <= begin)
				break;
			int end = index;
			array.add(str.substring(begin, end));
			begin = index + delimiter.length();
		}
		if ((begin >= 0) && (begin < str.length()))
			array.add(str.substring(begin));
		if (str.endsWith(delimiter))
			array.add("");
		if (array.size() > 0) {
			result = new String[array.size()];
			array.toArray(result);
		}
		return result;
	}

	public static String[] splitWithoutEmpty(String str, String delimiter) {
		ArrayList array = new ArrayList();
		int index = 0;
		int begin = 0;
		String[] result = new String[0];
		if (isEmpty(str))
			return new String[0];
		while (true) {
			index = str.indexOf(delimiter, begin);
			if (index == begin) {
				if (index > 0)
					array.add("");
				begin += delimiter.length();
			}

			if (index <= begin)
				break;
			int end = index;
			array.add(str.substring(begin, end));
			begin = index + delimiter.length();
		}
		if ((begin >= 0) && (begin < str.length()))
			array.add(str.substring(begin));
		if (array.size() > 0) {
			result = new String[array.size()];
			array.toArray(result);
		}
		return result;
	}

	public static String reverse(String str) {
		if (isEmpty(str)) {
			return str;
		}
		StringBuffer buffer = new StringBuffer(str);
		buffer.reverse();
		return buffer.toString();
	}

	public static String quote(String str) {
		if (isEmpty(str))
			return "\"\"";
		StringBuffer buffer = new StringBuffer(str);
		if (!str.startsWith("\""))
			buffer.insert(0, "\"");
		if (!str.endsWith("\""))
			buffer.append("\"");
		return buffer.toString();
	}

	public static String extractQuotedStr(String str) {
		if (isEmpty(str))
			return str;
		StringBuffer buffer = new StringBuffer(str);
		int index = str.length();
		while (buffer.charAt(buffer.length() - 1) == '"') {
			buffer.deleteCharAt(buffer.length() - 1);
			index = buffer.length();
			if (index <= 0)
				break;
		}
		if (buffer.length() == 0)
			return "";
		do {
			buffer.deleteCharAt(0);
			index = buffer.length();
			if (index <= 0)
				break;
		} while (buffer.charAt(0) == '"');

		return buffer.toString();
	}

	public static String strChar(String str, char c) {
		if ((str == null) || (str.length() == 0))
			return null;
		for (int i = 0; i < str.length(); ++i) {
			if (str.charAt(i) == c)
				return str.substring(i);
		}
		return null;
	}

	public static String strRChar(String str, char c) {
		if ((str == null) || (str.length() == 0))
			return null;
		for (int i = str.length() - 1; i >= 0; --i) {
			if (str.charAt(i) == c)
				return str.substring(i);
		}
		return null;
	}

	public static String[] toArray(Object[] array) {
		if ((array == null) || (array.length == 0))
			return null;
		String[] result = new String[array.length];
		for (int i = 0; i < array.length; ++i) {
			if (array[i] != null)
				result[i] = array[i].toString();
		}
		return result;
	}

	public static String[] toArray(Vector list) {
		if ((list == null) || (list.size() == 0))
			return null;
		String[] result = new String[list.size()];
		for (int i = 0; i < list.size(); ++i) {
			Object obj = list.get(i);
			if (obj != null) {
				result[i] = list.get(i).toString();
			}
		}
		return result;
	}

	public static List copyToList(String[] array, List list, int index) {
		if ((array == null) || (array.length == 0))
			return list;
		if ((list == null) || (index < 0))
			return list;
		for (int i = 0; i < array.length; ++i) {
			if (list.size() <= i + index)
				list.add(index + i, array[i]);
			else
				list.set(index + i, array[i]);
		}
		return list;
	}

	public static boolean contains(Object[] array, String str) {
		if ((array == null) || (array.length == 0))
			return false;
		if (str == null)
			return false;
		for (int i = 0; i < array.length; ++i) {
			Object obj = array[i];
			if ((obj != null) && (str.equals(obj.toString()))) {
				return true;
			}
		}
		return false;
	}

	public static int indexOf(Object[] array, String str) {
		if ((array == null) || (array.length == 0))
			return -1;
		if (str == null)
			return -1;
		for (int i = 0; i < array.length; ++i) {
			Object obj = array[i];
			if ((obj != null) && (str.equals(obj.toString()))) {
				return i;
			}
		}
		return -1;
	}

	public static boolean isValidEmail(String theEmail) {
		boolean cbool = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(theEmail);
			boolean isMatched = matcher.matches();
			if (isMatched)
				cbool = true;
		} catch (Exception e) {
			e.printStackTrace();
			return cbool;
		}
		return cbool;
	}

	public static String trimLeft(String str) {
		if (str == null)
			return null;
		int length = str.length();
		if (length == 0)
			return "";
		StringBuffer buffer = new StringBuffer(str);
		int index;
		for (index = 0; index < length && buffer.charAt(index) == ' '; index++)
			;
		if (index == length)
			return "";
		else
			return buffer.substring(index);
	}

	public static String trimRight(String str) {
		if (str == null)
			return null;
		int length = str.length();
		if (length == 0)
			return "";
		StringBuffer buffer = new StringBuffer(str);
		int index;
		for (index = length - 1; index >= 0 && buffer.charAt(index) == ' '; index--)
			;
		if (index < 0 && buffer.charAt(0) == ' ')
			return "";
		else
			return buffer.substring(0, index + 1);
	}

	public static String trimAll(String str) {
		return str.trim();
	}

	public static String removeNull(String str) {
		return (str != null) ? str : "";
	}

	public static String removeNull(Object str) {
		return (str != null) ? str.toString() : "";
	}

	public static boolean strInArray(String[] strs, String str, boolean caseSensitive) {
		if ((strs != null) && (strs.length > 0)) {
			for (int i = 0; i < strs.length; ++i) {
				if (caseSensitive) {
					if (strs[i].equals(str))
						return true;
				} else if (strs[i].equalsIgnoreCase(str)) {
					return true;
				}
			}
		}

		return false;
	}

	public static boolean idCardVerify(String idcard) {
		if (idcard.length() == 15) {
			idcard = idCardUptoeighteen(idcard);
		}
		if (idcard.length() != 18) {
			return false;
		}
		String verify = idcard.substring(17, 18);

		return verify.equals(getIdCardVerify(idcard));
	}

	public static String getIdCardVerify(String eightcardid) {
		int remaining = 0;
		if (eightcardid.length() == 18) {
			eightcardid = eightcardid.substring(0, 17);
		}
		if (eightcardid.length() == 17) {
			int sum = 0;
			for (int i = 0; i < 17; ++i) {
				String k = eightcardid.substring(i, i + 1);
				ai[i] = Integer.parseInt(k);
			}
			for (int i = 0; i < 17; ++i) {
				sum += wi[i] * ai[i];
			}
			remaining = sum % 11;
		}
		return (remaining == 2) ? "X" : String.valueOf(vi[remaining]);
	}

	public static String idCardUptoeighteen(String fifteencardid) {
		if (fifteencardid.length() != 15)
			return null;
		String eightcardid = fifteencardid.substring(0, 6);
		eightcardid = eightcardid + "19";
		eightcardid = eightcardid + fifteencardid.substring(6, 15);
		eightcardid = eightcardid + getIdCardVerify(eightcardid);
		return eightcardid;
	}

	public static boolean isPhoneNum(String phoneCode) {
		Pattern p = Pattern.compile("[0][1-9]{2,3}[1-9]{6,8}");
		Matcher m = p.matcher(phoneCode);
		boolean b = m.matches();
		return b;
	}

	public static String arrayToString(String[] str) {
		if (str == null)
			return "";
		StringBuffer rStr = new StringBuffer("");
		for (int i = 0; i < str.length; ++i) {
			rStr.append(str[i]);
			rStr.append(",");
		}

		if (rStr.toString().length() > 0) {
			rStr.setLength(rStr.length() - 1);
		}
		return rStr.toString();
	}

	public static boolean isNullOrEmpty(Object o) {
		return (o == null) || (String.valueOf(o).trim().length() == 0);
	}
}
